package com.ds.esd.bpm.worklist;


import com.ds.bpm.client.ProcessInst;
import com.ds.bpm.client.data.SearchData;
import com.ds.bpm.engine.BPMException;
import com.ds.bpm.engine.WorkflowClientService;
import com.ds.bpm.engine.query.BPMCondition;
import com.ds.bpm.engine.query.BPMConditionKey;
import com.ds.bpm.enums.process.ProcessInstStatus;
import com.ds.bpm.enums.right.RightConditionEnums;
import com.ds.common.JDSException;
import com.ds.common.query.Operator;
import com.ds.config.ErrorListResultModel;
import com.ds.config.ListResultModel;
import com.ds.context.JDSActionContext;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.bpm.worklist.view.ProcessInstListView;
import com.ds.esd.client.ESDClient;
import com.ds.esd.client.ESDFacrory;
import com.ds.esd.custom.annotation.CustomAnnotation;
import com.ds.esd.custom.annotation.GridViewAnnotation;
import com.ds.esd.custom.buttonviews.annotation.ButtonViewsAnnotation;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.tool.ui.enums.VAlignType;
import com.ds.jds.core.esb.EsbUtil;
import com.ds.server.JDSClientService;
import com.ds.server.JDSServer;
import com.ds.web.util.PageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/bpm/processInst/list/")
@MethodChinaName(cname = "工作流列表", imageClass = "bpmfont bpmgongzuoliuzhutiguizeweihuguanli")
@ButtonViewsAnnotation(barVAlign = VAlignType.bottom)

public class ProcessInstListService {


    @CustomAnnotation(uid = true, hidden = true)
    String projectId;


    public String getProjectId() {
        return projectId;
    }

    public void setProjectId(String projectId) {
        this.projectId = projectId;
    }

    @MethodChinaName(cname = "归档文件")
    @GridViewAnnotation(editorPath = "bpm.custom.ActivityInstDisplay")
    @ModuleAnnotation(imageClass = "bpmfont bpmlishihistory2", caption = "归档文件")
    @RequestMapping(method = RequestMethod.POST, value = "CompletedProcessInstList")
    public @ResponseBody
    ListResultModel<List<ProcessInstListView>> getCompletedProcessInstList(@RequestBody SearchData data) {
        ListResultModel<List<ProcessInstListView>> result = new ListResultModel<List<ProcessInstListView>>();
        try {
            BPMCondition condition = new BPMCondition(BPMConditionKey.PROCESSINST_STATE,
                    Operator.EQUALS, ProcessInstStatus.completed);
            ListResultModel<List<ProcessInst>> processList = getClient().getProcessInstList(condition, RightConditionEnums.CONDITION_COMPLETEDWORK, null, null);
            result = PageUtil.changPageList(processList, ProcessInstListView.class);
        } catch (BPMException e) {
            result = new ErrorListResultModel();
            ((ErrorListResultModel<List<ProcessInstListView>>) result).setErrdes(e.getMessage());
            ((ErrorListResultModel<List<ProcessInstListView>>) result).setErrcode(e.getErrorCode());
            e.printStackTrace();
        }

        return result;

    }

    /**
     * @return
     */
    public ESDClient getEsdClient() throws JDSException {
        ESDClient client = ESDFacrory.getESDClient();
        return client;
    }


    /**
     * @return
     */
    public WorkflowClientService getClient() {
        JDSClientService service = JDSActionContext.getActionContext().Par(JDSClientService.class);
        if (service == null) {
            try {
                JDSActionContext.getActionContext().getSession().put("JDSUSERID", JDSServer.getInstance().getAdminUser().getId());
            } catch (JDSException e) {
                e.printStackTrace();
            }
        }
        WorkflowClientService client = ((WorkflowClientService) EsbUtil.parExpression("$BPMC"));
        return client;
    }
}
